package org.stream.answerduel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stream.answerduel.entity.Friend;
import org.stream.answerduel.entity.User;
import org.stream.answerduel.repository.FriendRepository;
import org.stream.answerduel.repository.UserRepository;
import org.stream.answerduel.util.SnowflakeIdWorker;
import org.stream.answerduel.vo.UserVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 郭榕
 * @date 2019/7/17 15:57
 *
 *
 */
@Service
public class FriendService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private FriendRepository friendRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    /**
     * @author 郭榕
     * @date 2019/7/17 16:04
     * 功能描述: 保存好友
     * @param userId
     * @param friendId
     */
    public String saveFriend(Long userId , Long friendId){
        if(this.findFriend(userId,friendId)!=null)
        {
            return "好友已存在";
        }
        else
        {
            //friendRepository.delete(friendRepository.findFriendByUserIdAndFriendId(friendId,userId));

            Friend friend = friendRepository.findFriendByUserIdAndFriendId(friendId, userId);

            friend.setFlag(true);
            friend.setUpdateBy(userId);
            friend.setUpdateAt(new Date());

            //交叉添加好友
            Friend friend2 = new Friend();
            friend2.setUserId(userId);
            friend2.setFriendId(friendId);
            friend2.setFlag(true);
            friend2.setId(snowflakeIdWorker.nextId());
            friend2.setCreateBy(userId);
            friend2.setCreateAt(new Date());
            friend2.setUpdateBy(userId);
            friend2.setUpdateAt(new Date());

            friendRepository.save(friend);
            friendRepository.save(friend2);

            return "插入成功";
        }
    }

    /**
     * @author 郭榕
     * @date 2019/7/17 16:13
     * 功能描述：删除好友
     * @param userId
     * @param friendId
     */
    public void deleteFriend(Long userId , Long friendId){
        //级联删除
        friendRepository.delete(friendRepository.findFriendByUserIdAndFriendId(userId,friendId));
        friendRepository.delete(friendRepository.findFriendByUserIdAndFriendId(friendId,userId));
    }

    /**
     * @author 郭榕
     * @date 2019/7/18 15:33
     * 功能描述：根据userid和friendid查询好友
     *
     */
    public Friend findFriend(Long userId , Long friendId){
        return friendRepository.findFriendByUserIdAndFriendId(userId,friendId);
    }

    /**
     * @author 郭榕
     * @date 2019/7/18 16:00
     * 功能描述：申请好友，添加数据库并把flag设成0;
     */
    public String applyFriend(Long userId , Long friendId){

        if(this.findFriend(userId,friendId)!=null)
        {
            return "好友已存在,或申请已发送";
        }
        else{
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            friend.setFlag(false);
            friend.setId(snowflakeIdWorker.nextId());
            friend.setCreateBy(userId);
            friend.setCreateAt(new Date());
            friend.setUpdateBy(userId);
            friend.setUpdateAt(new Date());
            friendRepository.save(friend);

            return "申请成功,flag设0,数据库插入一条数据";
        }
    }

    /**
     * @author 郭榕
     * @date 2019/7/22 18:42
     * 功能描述：查看有谁申请加我为好友，也即是查询数据库falg为0的字段所对应的用户,此间我自己的id为friendId
     *
     */
    public List<UserVo> checkApply(Long userId){

        List<User> userList = userRepository.findApplyMe(userId);
        List<UserVo> userVoList = new ArrayList<UserVo>();
        for(User u : userList )
        {
            userVoList.add(new UserVo(u));
        }
        return userVoList;
    }

    /**
     *
     * @author 郭榕
     * @date 2019/7/22 18:58
     *功能描述：拒绝申请好友，也即是删除数据库falg为0的字段,此间我自己的id为friendId
     */
    public void refuseApply(Long userId , Long friendId)
    {
        friendRepository.delete(friendRepository.findFriendByUserIdAndFriendId(friendId,userId));
    }

}
